package com.mall.utils;

import com.mall.db.domain.Member;
import org.springframework.util.StringUtils;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

public class LoginInterceptor implements HandlerInterceptor {

    @Override
    public boolean preHandle(HttpServletRequest request,
                             HttpServletResponse response,
                             Object handler) throws Exception {
        HttpSession session =request.getSession();
        Member m = (Member) session.getAttribute("user");
        if (StringUtils.isEmpty(m)){
            String alert = "<script>" +
                    "alert('登录信息已过期,请重新登录');" +
                    "parent.location.href='/page/login'"+
                    "</script>";
            response.setContentType("text/html;charset=UTF-8");
            response.getWriter().println(alert);
            //重定向回到登录界面
            //request.getRequestDispatcher("/system/login").forward(request,response);
            return false;
        }
        return true;
    }
}
